<?php
// +----------------------------------------------------------------------
// | Niucloud-admin 企业快速开发的多应用管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.niucloud.com
// +----------------------------------------------------------------------
// | niucloud团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: Niucloud Team
// +----------------------------------------------------------------------

namespace app\service\core\order;

use app\api\model\order\OrderDelivery;
use app\dict\member\MemberAccountTypeDict;
use app\dict\order\OrderDeliveryDict;
use app\dict\order\OrderDict;
use app\dict\order\OrderGoodsDict;
use app\api\model\order\Order;
use app\api\model\order\OrderGoods;
use app\service\core\member\CoreMemberAccountService;
use app\service\core\order\CoreOrderEventService;
use app\core\base\BaseCoreService;
use app\core\exception\CommonException;

/**
 *  订单完成服务层
 */
class CoreOrderReturnService extends BaseCoreService
{

    use CoreOrderCreateTrait;
    public function __construct()
    {
        parent::__construct();
        $this->model = new Order();
    }


    /**
     * 订单协商
     * @param $data
     * @return true
     */
    public function return($order, $afterData, $data)
    {
        //参数赋值
        $this->setParam($data);
        //选中收货地址
        $this->selectTakeAddress();
        //校验错误
        $this->checkError();
        $update_data = array(
            'status' => OrderDict::RETURNED,
            'timeout' => 0,
        );
        $this->model->where('order_id',$order['order_id'])->update($update_data);
        // 物流信息
        // 发货相关
        $delivery_data = [
            'order_id' => $order['order_id'],
            'type' => 2,
            'express_company_id' => $data['express_company_id'],
            'express_number' => $data['express_number'],
            'delivery_type' => $this->delivery['delivery_type'] ?? '',
            'send_name' => $this->delivery['take_address']['name'] ?? '',
            'send_mobile' => $this->delivery['take_address']['mobile'] ?? '',
            'send_province' => $this->delivery['take_address']['province_id'] ?? 0,
            'send_city' => $this->delivery['take_address']['city_id'] ?? 0,
            'send_district' => $this->delivery['take_address']['district_id'] ?? 0,
            'send_address' => $this->delivery['take_address']['address'] ?? '',
            'send_full_address' => $this->delivery['take_address']['full_address'] ?? '',
            'send_longitude' => $this->delivery['take_address']['lng'] ?? '',
            'send_latitude' => $this->delivery['take_address']['lat'] ?? '',
            'send_store_id' => $this->delivery['take_store']['store_id'] ?? 0,
        ];
        (new OrderDelivery())->create($delivery_data);

        //订单完成后操作
        CoreOrderEventService::orderReturnAfter($afterData);
        return true;
    }


}
